Ext.define('TS.view.inv.InventoryList' ,{
    extend: 'Ext.grid.Panel',
    alias : 'widget.inventorylist',
    title : '商品库存',
    columnLines: true,
    bodyPadding:0,
    frame : true,//面板渲染
    closable: true,
    
    /* fields: ['id','invAddId','productId','batchNoId','fyear',
    'period', 'beginQty','beginAmt','inQty','inAmt',
    'outQty', 'outAmt','costPrice','waitInQty',
    'waitOutQty', 'state','remainQty','remainAmt','usableQty'
    ]*/
    columns:[
   			{header: '存货地址',width:60,
            	renderer:function(value,metaData,record){
					if(Ext.isEmpty(record.raw.inventoryAdd))
            			return '';
					return record.raw.inventoryAdd.inventoryAddName;
				}
			},
            {header: '商品编号',width:60,
            	renderer:function(value,metaData,record){
					if(Ext.isEmpty(record.raw.product))
            			return '';
					return record.raw.product.productCode;
				}
			},
			{header: '商品名称',width:200,
            	renderer:function(value,metaData,record){
					if(Ext.isEmpty(record.raw.product))
            			return '';
					return record.raw.product.productName;
				}
			},
			{header: '规格',width:60,
            	renderer:function(value,metaData,record){
					if(Ext.isEmpty(record.raw.product))
            			return '';
					return record.raw.product.packSpec;
				}
			},
			{header: '生产企业',width:100,
            	renderer:function(value,metaData,record){
					if(Ext.isEmpty(record.raw.product))
            			return '';
					return record.raw.product.manuf;
				}
			},
			{header: '单位',width:40,
            	renderer:function(value,metaData,record){
					if(Ext.isEmpty(record.raw.product))
            			return '';
					return record.raw.product.unit;
				}
			},
            {header: '批号',width:80,
            	renderer:function(value,metaData,record){
					if(Ext.isEmpty(record.raw.batchNo))
            			return '';
					return record.raw.batchNo.batchNo;
				}
			},
			{header: '生产日期',width:80,
            	renderer:function(value,metaData,record){
					if(Ext.isEmpty(record.raw.batchNo))
            			return '';
					return record.raw.batchNo.manufDate;
				}
			},
			{header: '有效期',width:80,
            	renderer:function(value,metaData,record){
					if(Ext.isEmpty(record.raw.batchNo))
            			return '';
					return record.raw.batchNo.expDate;
				}
			},
            {header: '期初数量', dataIndex: 'beginQty', width:80},
            {header: '期初金额', dataIndex: 'beginAmt',width:80},
            {header: '本期入库', dataIndex: 'inQty',width:80},
            {header: '入库金额', dataIndex: 'inAmt',width:80},
            {header: '本期出库', dataIndex: 'outQty',width:80},
            {header: '出库金额', dataIndex: 'outAmt',width:80},
            {header: '成本价', dataIndex: 'outAmt',width:80},
            {header: '待入库数', dataIndex: 'waitInQty',width:80},
            {header: '待出库数', dataIndex: 'waitOutQty',width:80},
            {header: '可用库存', dataIndex: 'usableQty',width:80},
            {header: '库存数量', dataIndex: 'remainQty',width:80},
            {header: '库存金额', dataIndex: 'remainAmt',width:80},
            {header: '状态', dataIndex: 'state',width:50,
            	renderer:function(value,metaData,record){
            		if(value==0)
            			return '可用'
            		else
            			return '不可用'
            	}
            }
    ],
    tbar :[
				{xtype:'button',action:'addnew',text:'添加商品',iconCls:'bill-add-button'},
				{xtype:'button',action:'delzero',text:'删除商品',iconCls:'bill-delete-button'},
				{xtype:'button',action:'lock',text:'禁用',iconCls:'bill-archive-button'},
				{xtype:'button',action:'unlock',text:'启用',iconCls:'bill-unarchive-button'},
				{
					xtype: 'triggerfield',
					labelWidth :50,
					fieldLabel:'查询:',
					width:250,
					margin:'0,0,10,10',
					emptyText:'请输入商品编码或名称',
					trigger1Cls:Ext.baseCSSPrefix + "form-search-trigger",
					onTriggerClick:function(e){
						e.keyCode=e.ENTER;//设置为Enter事件
						this.fireEvent('specialkey',this,e);//点客户查询图标执行搜索
					}
				},
				{xtype:'button',action:'exportxls',text:'导出',iconCls:'bill-export-button'},
				{xtype:'button',action:'ref',text:'刷新',iconCls:'bill-ref-button'}
		   ],
	
	selType:'checkboxmodel',//设定选择模式
	multiSelect:true,//运行多选
	initComponent:function(){
		var me=this;
		var st=Ext.create('TS.store.inv.InventoryStore');
		
		this.store=st;
		this.dockedItems =[{
				xtype:'pagingtoolbar',
				store:st,
				dock:'bottom',
				displayInfo:true
		}],
		this.callParent(arguments);
	},
	    /** 
     * Kunoy 
     * 合并单元格 
     * @param {} grid  要合并单元格的grid对象 
     * @param {} cols  要合并哪几列 [1,2,4] 
     */  
     mergeCells:function(grid,cols){  
     	var table=document.getElementById(Ext.get(grid.getId()+"-body").down('table').id);
        //var arrayTr=document.getElementById(grid.getId()+"-body").firstChild.firstChild.firstChild.getElementsByTagName('tr'); 
        var arrayTr=table.getElementsByTagName('tr'); 
        var trCount = arrayTr.length;  
        var arrayTd;  
        var td;  
        var merge = function(rowspanObj,removeObjs){ //定义合并函数  
            if(rowspanObj.rowspan != 1){  
                arrayTd =arrayTr[rowspanObj.tr].getElementsByTagName("td"); //合并行  
                td=arrayTd[rowspanObj.td-1];  
                td.rowSpan=rowspanObj.rowspan;  
                td.vAlign="middle";               
                Ext.each(removeObjs,function(obj){ //隐身被合并的单元格  
                    arrayTd =arrayTr[obj.tr].getElementsByTagName("td");  
                    arrayTd[obj.td-1].style.display='none';                           
                });  
            }     
        };    
        var rowspanObj = {}; //要进行跨列操作的td对象{tr:1,td:2,rowspan:5}      
        var removeObjs = []; //要进行删除的td对象[{tr:2,td:2},{tr:3,td:2}]  
        var col;  
        Ext.each(cols,function(colIndex){ //逐列去操作tr  
            var rowspan = 1;  
            var divHtml = null;//单元格内的数值          
            for(var i=0;i<trCount;i++){  //i=0表示表头等没用的行  
                arrayTd = arrayTr[i].getElementsByTagName("td");  
                var cold=0;  
    //          Ext.each(arrayTd,function(Td){ //获取RowNumber列和check列  
    //              if(Td.getAttribute("class").indexOf("x-grid-cell-special") != -1)  
    //                  cold++;                               
    //          });  
                col=colIndex+cold;//跳过RowNumber列和check列  
                if(!divHtml){  
                    divHtml = arrayTd[col-1].innerHTML;  
                    rowspanObj = {tr:i,td:col,rowspan:rowspan}  
                }else{  
                    var cellText = arrayTd[col-1].innerHTML;  
                    var addf=function(){   
                        rowspanObj["rowspan"] = rowspanObj["rowspan"]+1;  
                        removeObjs.push({tr:i,td:col});  
                        if(i==trCount-1)  
                            merge(rowspanObj,removeObjs);//执行合并函数  
                    };  
                    var mergef=function(){  
                        merge(rowspanObj,removeObjs);//执行合并函数  
                        divHtml = cellText;  
                        rowspanObj = {tr:i,td:col,rowspan:rowspan}  
                        removeObjs = [];  
                    };  
                    if(cellText == divHtml){  
                        if(colIndex!=cols[0]){   
                            var leftDisplay=arrayTd[col-2].style.display;//判断左边单元格值是否已display  
                            if(leftDisplay=='none')  
                                addf();   
                            else  
                                mergef();                             
                        }else  
                            addf();                                           
                    }else  
                        mergef();             
                }  
            }  
        });
    }
});
